package com.mapre.instance.friend;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author shengtao
 * @Description:
 * @date 2019/01/14 18:11
 */
public class SecondMR extends Configured implements Tool {

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new SecondMR(),args);
        System.out.println("map reducer processed success!");
    }

    @Override
    public int run(String[] args) throws Exception {

        String nameNodeUrl = "hdfs://192.168.220.135:9000";
        String nameNodeName = "fs.defaultFS";
        Configuration configuration = new Configuration();
        configuration.set(nameNodeName, nameNodeUrl);
        Job job = Job.getInstance(configuration);
        job.setJarByClass(FriendMR.class);

        job.setMapperClass(SecondMapper.class);
        job.setReducerClass(SecondReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean result = job.waitForCompletion(true);
        if (!result){
            return 1;
        }
        //======
        return 0;
    }

    static class SecondMapper extends Mapper<LongWritable,Text,Text,Text>{

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();

            String friend_users[] = line.split("\t");

            String friend = friend_users[0];

            String[] users = friend.split(",");

            Arrays.sort(users);

            for (int i=0;i<users.length - 1;i++){
                for (int j=i+1;j<users.length;j++){
                    //这两人有共同好友
                    context.write(new Text(users[i]+"-"+users[j]),new Text(friend));
                }
            }
        }
    }

    static class SecondReducer extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder sb = new StringBuilder();
            for (Text friend:values){
                sb.append(friend.toString()).append(" ");
            }
            context.write(key,new Text(sb.toString()));
        }
    }
}
